package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.apps.ftapp.R;
import com.apps.ftapp.model.LessonModel;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by lau on 2018/3/15.
 */

public class LessonNewAdapter extends ArrayAdapter<LessonModel.DataBean> {
    private int resource;
    private Context context;
    private List<LessonModel.DataBean> list;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;

    public LessonNewAdapter(Context context, int resource, List<LessonModel.DataBean> objects) {
        super(context, resource, objects);
        this.resource = resource;
        this.context = context;
        this.list = objects;

        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    }

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LessonModel.DataBean bean = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(resource, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.lessonImg = view.findViewById(R.id.lesson_img);
            viewHolder.lessonTitle = view.findViewById(R.id.lesson_title);
            viewHolder.lessonPrice = view.findViewById(R.id.lesson_price);
            viewHolder.lessonTime = view.findViewById(R.id.lesson_time);
            viewHolder.lessonSale = view.findViewById(R.id.lesson_sale);
            viewHolder.lessonXiangou = view.findViewById(R.id.lesson_xiangou);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.lessonTitle.setText(bean.getTitle());
        viewHolder.lessonPrice.setText("¥" + bean.getPrice());
        viewHolder.lessonTime.setText(bean.getNew_startTime());
        viewHolder.lessonSale.setText("已购" + bean.getStudentNum());
        viewHolder.lessonXiangou.setText("限购" + bean.getMaxStudentNum());
        imageLoader.displayImage(bean.getLargePicture(), viewHolder.lessonImg, options);
        return view;
    }

    class ViewHolder {
        ImageView lessonImg;
        TextView lessonTitle;
        TextView lessonTime;
        TextView lessonSale;
        TextView lessonPrice;
        TextView lessonXiangou;
    }
}
